
// <editor-fold defaultstate="collapsed" desc="import">
package com.compuvest.ajax;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;

import java.util.logging.Logger;
import java.util.logging.Level;

import com.compuvest.web.SQLBean;
import com.compuvest.web.Mail;
// </editor-fold>

@WebServlet(name="DropPriceAlert",loadOnStartup=1,urlPatterns={"/droprice"})
public class DropPriceAlert extends HttpServlet {

    private final static Logger log = Logger.getLogger(DropPriceAlert.class.getName());

    private SQLBean mysql;
    private Mail mail;

// <editor-fold defaultstate="collapsed" desc="init()">

    @Override
    public void init(ServletConfig config) throws ServletException {
	super.init(config);

        mysql = (SQLBean)config.getServletContext().getAttribute("sb");
	if(mysql == null) {
            log.log(Level.SEVERE, "mysql=null");
        }
        mail = (Mail)config.getServletContext().getAttribute("mail");
	if(mail == null) {
            log.log(Level.SEVERE, "mail=null");
        }
        log.log(Level.INFO, "Init");
    }

// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="processRequest()">

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {

        String action = request.getParameter("act");
        String json = "";
	if(action != null) {
            String itemID = request.getParameter("iid");
            switch (action) {
                case "it":
                    json = mysql.getItemDropPriceAlert(itemID);
                    break;
                case "sd":
                    // send email
                    mail.sendDropPrice(
                        itemID,
                        request.getParameter("name"),
                        request.getParameter("phone"),
                        request.getParameter("email")
                    );
                    break;
                case "qr":
                    // quote request (OnSiteServices.jsp)
                    mail.quoteRequest(request.getParameter("ms"));
                    break;
            }
	}

        response.setContentType("text/html;charset=UTF-8");
        try(PrintWriter out = response.getWriter()) {
            out.print(json);
        }
   }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods.">
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }
    @Override
    public String getServletInfo() {
        return "DropPriceAlert";
    }
    @Override
    public void destroy() {
        log.log(Level.INFO, "Destroy");
    }
// </editor-fold>

}
